import axios, { AxiosRequestConfig } from "axios"
import { config, requestInterceptor, responseInterceptor } from "~/config/axios"
import { Response } from "~/types/api/response";

const request = (option: AxiosRequestConfig) => new Promise<Response>((resolve, reject) => {
    const instance = axios.create(config);

    instance.interceptors.request.use(requestInterceptor)
    instance.interceptors.response.use(responseInterceptor)

    return instance.request(option).then(response => {
        if (response.data.status == 'success') {
            resolve(response.data);
        } else {
            console.error(response);
            reject(
                new Error(response.data.message)
            )
        }
    }).catch(error => {
        reject(error);
    })
})
export { request }